//测试动画



import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ScaleAnimationRoute extends StatefulWidget{

   const ScaleAnimationRoute({Key? key}):super(key: key);

  @override
  State<StatefulWidget> createState() => _ScaleAnimationRouteState();

}


class _ScaleAnimationRouteState  extends State with SingleTickerProviderStateMixin{

  late AnimationController controller;
  late Animation<double> animation;


  @override
  Widget build(BuildContext context) {

      return Center(
        child: Image.network("https://img0.baidu.com/it/u=3365573645,2073973856&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
           width: animation.value,
            height: animation.value,
        ),

      );
  }

  @override
  void initState() {
   super.initState();
     controller = AnimationController( duration: const Duration(seconds: 2),vsync: this);
     animation = CurvedAnimation(parent: controller, curve: Curves.bounceIn);
     animation = Tween(begin: 0.0,end: 300.0).animate(controller)
   ..addListener(() {
           setState(() {

           });
   });
     controller.forward();
}

   @override
  void dispose() {
     //路由销毁时需要释放动画资源
     controller.dispose();
     super.dispose();
  }

}

class AnimatedImage extends AnimatedWidget{

  AnimatedImage({required Animation<double> animation, }) : super(listenable: animation);

  @override
  Widget build(BuildContext context) {

    final animation = listenable as Animation<double>;

    return Center(
      child: Image.network("https://img0.baidu.com/it/u=3365573645,2073973856&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
        width: animation.value,
        height: animation.value,
      ),

    );
  }
}

class ScaleAnimationRoute1  extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => _ScaleAnimationRouteState1();

}

class _ScaleAnimationRouteState1  extends State with SingleTickerProviderStateMixin{

  late Animation<double> animation;
  late AnimationController controller;

  @override
  Widget build(BuildContext context) {
        return AnimatedImage(animation: animation);
  }

  @override
  void initState() {
    super.initState();

    controller = AnimationController( duration: const Duration(seconds: 2), vsync: this);

    animation = Tween(begin: 0.0, end: 300.0).animate(controller);
    //启动动画
    controller.forward();
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();

  }
}

class ScaleAnimationBuildRoute1 extends StatefulWidget{

  @override
  State<StatefulWidget> createState() => _ScaleAnimationBuildRouteState1();

}

class _ScaleAnimationBuildRouteState1 extends State with SingleTickerProviderStateMixin{

  late Animation<double> animation;
  late AnimationController controller;

   @override
  void initState() {

     controller =  AnimationController(duration:const Duration(seconds: 2),vsync: this);
     animation =  Tween(begin: 0.0, end: 300.0).animate(controller)
     ..addStatusListener((status) {

       print("status--->"+status.toString());

       // if (status == AnimationStatus.completed) {
       //   //动画执行结束时反向执行动画
       //   controller.reverse();
       // } else if (status == AnimationStatus.dismissed) {
       //   //动画恢复到初始状态时执行动画（正向）
       //   controller.forward();
       // }
     });
     controller.forward();
  }


  @override
  Widget build(BuildContext context) {

      return AnimatedBuilder(
          animation: animation,
          child:Image.network("https://img0.baidu.com/it/u=3365573645,2073973856&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500"),
          builder: (context,child) {
            return Center(
              child: SizedBox(
                height: animation.value,
                width: animation.value,
                child: child,
              ),
            );
          },
      );
  }
}








